package parroty;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;

import org.waveprotocol.wave.model.id.WaveId;
import org.waveprotocol.wave.model.id.WaveletId;

import com.google.wave.api.AbstractRobot;
import com.google.wave.api.Annotation;
import com.google.wave.api.Blip;
import com.google.wave.api.BlipContentRefs;
import com.google.wave.api.Gadget;
import com.google.wave.api.Range;
import com.google.wave.api.Wavelet;
import com.google.wave.api.event.GadgetStateChangedEvent;
import com.google.wave.api.event.WaveletParticipantsChangedEvent;
import com.google.wave.api.event.WaveletSelfAddedEvent;
import com.sun.xml.internal.bind.v2.TODO;

public class ParrotyServlet3 extends AbstractRobot {

	private static Logger mst_oLogger = Logger.getLogger(ParrotyServlet3.class
			.getName());

	private static String mst_sGadgetUrl = "http://people.msoe.edu/~leonharl/gadgets/wavedemo.xml";
	private static String mst_sRpcServer = "http://www-opensocial-sandbox.googleusercontent.com/api/rpc";

	public ParrotyServlet3() {
		// try
		// {
		//		
		setupOAuth("1064997498365", "1O2PBrHwIUK/D2MnmxpkXSaH", mst_sRpcServer);
		// TODO: what does this mean dif of true vs false
		setAllowUnsignedRequests(true);
		// log("Post OAth setup");
		// }
		// catch(Exception e)
		// {
		// log("Exception trying to setupOAuth "+e.getMessage());
		// }
	}

	// public ParrotyServlet3()
	// {
	// String verificationToken =
	// "AOijR2fqJrzeIF4jjIu_-rH0s5s6K7KpoTlhqtp1zltTzKKlYDQ_AmGjwg9g93BAbrC4sfp4O7eKFRzRYdbWaWgjfrmacshFNH9NPyDNDLIGevLjTng7UxpoeDAU6EG84UEnwpxm8-Me0wlJcmVE8Tkodl2Orv-YzA=="
	// ;
	// String securityToken = "4989";
	// setupVerificationToken(verificationToken, securityToken);
	// }

	@Override
	protected String getRobotName() {
		return "Parroty - is very cool";
	}

	@Override
	protected String getRobotAvatarUrl() {
		return "http://www.bradyid.com/favicon.ico";
	}

	@Override
	protected String getRobotProfilePageUrl() {
		return "http://code.google.com/apis/wave/extensions/robots/java-tutorial.html";
	}

	@Override
	public void onWaveletSelfAdded(WaveletSelfAddedEvent event) {
		Blip blip = event.getWavelet().reply("\nHi everybody!");

		// Gadget gadget = new
		// Gadget("http://my-wave-gadgets.appspot.com/wave/NapkinGadget.xml");
		// Gadget gadget = new Gadget(
		// "http://wave-api.appspot.com/public/gadgets/areyouin/gadget.xml");

		Gadget gadget = new Gadget(mst_sGadgetUrl);

		// gadget.setProperty("brc-app3-00@appspot.com:answer", "y");
		// gadget.setProperty("brc-app3-00@appspot.com:order", "0");

		event.getWavelet().getRootBlip().append(gadget);

	}

	@Override
	public void onWaveletParticipantsChanged(
			WaveletParticipantsChangedEvent event) {
		for (String newParticipant : event.getParticipantsAdded()) {
			Blip blip = event.getWavelet().reply("\nHi : " + newParticipant);
		}
	}

	@Override
	public void onGadgetStateChanged(GadgetStateChangedEvent event) {
		// TODO Auto-generated method stub

		BlipContentRefs content = event.getBlip().at(event.getIndex());
		Gadget gadget = (Gadget) content.value();

		// Make sure this is the yes no gadget"
		if (mst_sGadgetUrl.equals(gadget.getUrl())) {
			Set<Map.Entry<String, String>> entries = gadget.getProperties()
					.entrySet();

			Iterator<Map.Entry<String, String>> itr = entries.iterator();
			while (itr.hasNext()) {

				Map.Entry<String, String> entry = itr.next();
				event.getWavelet().reply(
						"\n" + entry.getKey() + "=" + entry.getValue());
			}
		}

		try {
			Wavelet oMyWavelet = fetchWavelet(new WaveId("wavesandbox.com",
					"w+6l0Hu8xvG"), new WaveletId("wavesandbox.com",
					"conv+root"), mst_sRpcServer);
			if (oMyWavelet != null) {
				event.getWavelet().reply("\n" + oMyWavelet.getTitle());

				event.getWavelet().reply("\nReady To Get Blips");

				for (Blip curBlip : oMyWavelet.getBlips().values()) {
					try {

						Blip newBlip = event.getWavelet().reply(
								"\n" + curBlip.getContent());

						for (Annotation curAnnotaion : curBlip.getAnnotations()) {
							Range anoRange = curAnnotaion.getRange();
							newBlip.range(anoRange.getStart(),
									anoRange.getEnd()).annotate(
									curAnnotaion.getName(),
									curAnnotaion.getValue());
						}
					} catch (Exception e) {
						event.getWavelet().reply(
								"\n" + "Cannot copy Annotation on blip "
										+ e.getMessage());
					}
				}
			} else {
				event.getWavelet().reply("\nMy fetched Wave was null");
			}

		} catch (Exception e) {
			event.getWavelet().reply(
					"\n" + "Cannot get new wave " + e.getMessage());
		}

	}

}
